;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Wednesday) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require "extras.rkt") (require rackunit) ;; posns-moved-by-posn : ListOfPosn Posn -> ListOfPosn ;; GIVEN: a list of posns lop and a posn delta ;; RETURNS: the list of positions that you get by adding delta coordinate-wise ;; to each of the posn in lop ;; EXAMPLE: ;; (list (x1 y1) (x2 y2) (x3 y3)) (dx, dy) => ;; (list (x1+dx, y1+dy) (x2+dx, y2+dy) (x3+dx, y3+dy)) ;; STRATEGY: SD on Posn (define (posns-moved-by-posn lop delta) (map (lambda (p) (make-posn (+ (posn-x p) (posn-x delta)) (+ (posn-y p) (posn-y delta)))) lop)) ;; Strategy: SD on delta : Posn (define (posns-moved-by-posn lop delta) (posns-moved-by-xy lop (posn-x delta) (posn-y delta))) ;; ListOfPosn Number Number -> ListOfPosn ;; STRATEGY: HOFC (define (posns-moved-by-xy lop dx dy) (map (lambda (p) (posn-moved-by-xy p dx dy)) lop)) ;; Posn Number Number -> Posn ;; STRATEGY: SD on p : Posn (define (posn-moved-by-xy p dx dy) (make-posn (+ (posn-x p) dx) (+ (posn-y p) dy))) ;; version 2: ;; HOFC (define (posns-moved-by-posn lop delta) (map (lambda (p) (posn-moved-by-posn p delta)) lop)) ;; SD on delta : Posn (define (posn-moved-by-posn p delta) (posn-moved-by-xy p (posn-x delta) (posn-y delta))) ;; version 3 (define (posns-moved-by-posn lop delta) (local ((define dx (posn-x delta)) (define dy (posn-y delta))) (map (lambda (p) (make-posn (+ (posn-x p) dx) (+ (posn-y p) dy))) lop))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Vedant ;; Avanti (define-struct mothership (name crew daughters)) ;; INTERPRETATION: ;; name is the name of the ship ;; crew is the list of crewmembers of the ship ;; daughters is the list of daughter ships (define-struct drone ()) ;; A Spaceship is one of - ;; -- (make-mothership String ListOfMartian ListOfShip) ;; OR ;; -- (make-drone) ;; Template ;; ship-fn : Spaceship -> ?? #|(define (ship-fn s) (cond [(mothership? s) (... (mothership-name s) (lom-fn (mothership-crew s)) (los-fn (mothership-daughters s)))] [(drone? s) ...])) |# ;; A ListofShip is either - ;; -- empty ;; -- (cons Spaceship ListOfShip) ;; Template ;; los-fn : ListOfShip -> ?? #| (define (los-fn los) (cond [(empty? los) ...] [else (... (ship-fn (first los)) (los-fn (rest los)))])) |# (define-struct martian (name)) ;; A Martian is a (make-martian String) ;; Template: ;; martian-fn : Martian -> ?? #|(define (martian-fn m) (... (martian-name m))) |# ;; A ListOfMartian is one of - ;; -- empty ;; -- (cons Martian (ListOfMartian)) ;; Template ;; lom-fn : ListOfMartian -> ?? #|(define (lom-fn lom) (cond [(empty? lom) ...] [else (... (martian-fn (first lom)) (lom-fn (rest lom)))])) |# ;; ship-name-count : Ship String -> NonNegInt ;; GIVEN: A Ship and a String ;; RETURNS: the number of Martians in the crew of the given ship or its fleet ;; with the given name. ;; STRATEGY: SD on Ship (define (ship-name-count s name) (cond [(mothership? s) (+ (crew-name-count (mothership-crew s) name) (los-name-count (mothership-daughters s) name))] [(drone? s) 0])) ;; crew-name-count : ListOfMartian String -> NonNegInt ;; RETURNS: the number of martians in the given list with the given name ;; STRATEGY: HOFC (define (crew-name-count lom name) (foldr ;; Maritan NonNegInt -> NonNegInt (lambda (m ans-for-rest) (if (martian-has-name? m name) (+ ans-for-rest 1) ans-for-rest)) 0 lom)) ;;foldr : (X Y -> Y) Y List -> Y ;; los-name-count : ListOfShip String -> NonNegInt ;; RETURNS: the number of martians in these ships and their fleets with the given name. ;; STRATEGY: HOFC (define (los-name-count los name) (foldr ;; Ship NonNegInt -> NonNegInt (lambda (s count) (+ (ship-name-count s name) count)) 0 los)) ;; OR: (define (los-name-count los name) (sum (map (lambda (s) (ship-name-count s name)) los))) (define (sum lon) (foldr + 0 lon)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #| Given a spaceship, returns another spaceship just like the original, except that all of its daughters who have a crewmember named "Mork" have been removed. When a daughter is removed from its daughters are adopted by their grandmother |# ;; Ship String -> Ship ;; STRATEGY: SD on s : Ship (define (ship-filter-fleet1 s name) (cond [(mothership? s) (make-mothership (mothership-name s) (mothership-crew s) (los-filter-fleet1 (mothership-daughters s) name))] [(drone? s) s])) ;; los-filter-fleet1 : ListOfShip String -> ListOfShip ;; GIVEN: a list of ships ;; RETURNS: a list like the original, except that any ship whose crew contains a martian ;; with the given name is replaced by its daughters ;; EXAMPLE: See Board. ;; STRATEGY: SD on los : ListOfShip (define (los-filter-fleet1 los name) (cond [(empty? los) empty] [else (if (ship-has-crew-named (first los) name) (append ;; ok to apply mothership-daughters here because if the ship has a crew, then ;; it must in fact be a mothership. (mothership-daughters (first los)) (los-filter-fleet1 (rest los) name)) (cons (first los) (los-filter-fleet1 (rest los) name)))])) ;; or with HOFC (define (los-filter-fleet1 los name) (foldr (lambda (first-of-los result-of-recursion) (if (ship-has-crew-named first-of-los name) (append ;; ok to apply mothership-daughters here because if the ship has a crew, then ;; it must in fact be a mothership. (mothership-daughters first-of-los) result-of-recursion) (cons first-of-los result-of-recursion))) empty los))